﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Maticsoft.Model;

namespace CallCenter.DAL
{
    partial class CustomersDAL
    {
        public CustomersModel GetByCarNum(string carNum)
        {
            DataTable table = SqlHelper.ExecuteDataTable("select * from T_Customers where CarNum=@carNum",
                new SqlParameter("@carNum", carNum));
            if (table.Rows.Count <= 0)
            {
                return null;
            }
            else
            {
                DataRow row = table.Rows[0];
                return ToCustomer(row);
            }
        }

        public int Update(CustomersModel model)
        {
            return SqlHelper.ExecuteNonQuery(@"update T_Customers set 
Name=@Name,NamePinyin=@NamePinyin,TelPhone=@TelPhone,MobilePhone=@MobilePhone,PostCode=@PostCode,Email=@Email,
Address=@Address,DeptNo=@DeptNo,BuyDate=@BuyDate,CarNum=@CarNum,BracketNum=@BracketNum,Brand=@Brand,TypeNum=@TypeNum,Suggestion=@Suggestion,Remarks=@Remarks where Id=@Id",
            new SqlParameter("@Id", model.Id),
            new SqlParameter("@Name", model.Name),
            new SqlParameter("@NamePinyin", model.NamePinyin),
            new SqlParameter("@TelPhone", model.TelPhone),
            new SqlParameter("@MobilePhone", model.MobilePhone),
            new SqlParameter("@PostCode", model.PostCode),
            new SqlParameter("@Email", model.Email),
            new SqlParameter("@Address", model.Address),
            new SqlParameter("@DeptNo", model.DeptNo),
            new SqlParameter("@BuyDate", model.BuyDate),
            new SqlParameter("@CarNum", model.CarNum),
            new SqlParameter("@BracketNum", model.BracketNum),
            new SqlParameter("@Brand", model.Brand),
            new SqlParameter("@TypeNum", model.TypeNum),
            new SqlParameter("@Suggestion", model.Suggestion),
            new SqlParameter("@Remarks", model.Remarks));
        }

        public CustomersModel[] GetByPinYin(string pinyin)
        {
           DataTable table = SqlHelper.ExecuteDataTable("select * from T_Customers where NamePinYin = @pinyin",
                new SqlParameter("@pinyin", pinyin));
           if (table.Rows.Count > 0)
           {
               CustomersModel[] customers = new CustomersModel[table.Rows.Count];
               for (int i = 0; i < table.Rows.Count; i++)
               {
                   DataRow row = table.Rows[i];
                   CustomersModel model = ToCustomer(row);
                   customers[i] = model;
               }
               return customers;
           }
           else
           {
               return null;
           }
        }
    }
}
